// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StrategyExpo.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The strategy expo
    /// </summary>
    public class StrategyExpo : Expo<IStrategyExpoCell>
    {
        private static readonly Identity Identity = Identity.Create(System.Reflection.MethodBase.GetCurrentMethod());

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StrategyExpo"/> class. 
        /// Initializes a new instance of the <see cref="InstrumentExpo"/> class.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        public StrategyExpo()
        {
            Logging.Log(Identity, ELogLevel.Debug, () => string.Format("Creating new StrategyExpo"));

            // Traded
            this[EExpoCellType.Traded] = new StrategyExpoCellTraded();

            // Working
            this[EExpoCellType.Working] = new StrategyExpoCellWorking();

            // Inventory
            this[EExpoCellType.Inventory] = new StrategyExpoCellInventory();

            // Target
            this[EExpoCellType.Target] = new StrategyExpoCellTarget();

            // Pending
            this[EExpoCellType.Pending] = new StrategyExpoCellPending();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Subscribe the instrument i to the strategyexpo
        /// </summary>
        /// <param name="i">
        /// </param>
        /// <returns>
        /// The add.
        /// </returns>
        public virtual bool Add(IInstrument i)
        {
            if (i == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Add : Cannot add Instrument, Invalid Instrument"));
                return false;
            }

            if (i.InstrumentStaticData == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Add : Cannot add Instrument, Invalid Instrument Static Data"));
                return false;
            }

            Logging.Log(
                Identity, 
                ELogLevel.Debug, 
                () => string.Format("Add : adding IInstrument {0} ({1})", i.InstrumentStaticData.Code, i.InstrumentStaticData.TradingScope));
            foreach (StrategyExpoCell cell in this.Cells)
            {
                if (cell != null)
                {
                    Logging.Log(
                        Identity, 
                        ELogLevel.Debug, 
                        () =>
                        string.Format(
                            "Add : adding strategy expo cell {0} , Instrument : {1} ({2})", 
                            cell.ExpoCellType, 
                            i.InstrumentStaticData.Code, 
                            i.InstrumentStaticData.TradingScope));
                    cell.Add(i);
                }
                else
                {
                    Logging.Log(
                        Identity, 
                        ELogLevel.Error, 
                        () =>
                        string.Format(
                            "Add : cannot add strategy expo cell, invalid cell : {0} ({1})", i.InstrumentStaticData.Code, i.InstrumentStaticData.TradingScope));
                }
            }

            return true;
        }

        /// <summary>
        /// Remove Instrument from this Expo
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The remove.
        /// </returns>
        public virtual bool Remove(IInstrument i)
        {
            if (i == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Remove : Cannot remove Instrument, Invalid Instrument"));
                return false;
            }

            if (i.InstrumentStaticData == null)
            {
                Logging.Log(Identity, ELogLevel.Error, () => string.Format("Remove : Cannot remove Instrument, Invalid Instrument Static Data"));
                return false;
            }

            foreach (StrategyExpoCell cell in this.Cells)
            {
                if (cell != null)
                {
                    Logging.Log(
                        Identity, 
                        ELogLevel.Debug, 
                        () =>
                        string.Format(
                            "Remove : removing strategy expo cell {0} , Instrument : {1} ({2})", 
                            cell.ExpoCellType, 
                            i.InstrumentStaticData.Code, 
                            i.InstrumentStaticData.TradingScope));
                    cell.Remove(i);
                }
                else
                {
                    Logging.Log(
                        Identity, 
                        ELogLevel.Error, 
                        () =>
                        string.Format(
                            "Remove : cannot remove strategy expo cell, invalid cell : {0} ({1})", 
                            i.InstrumentStaticData.Code, 
                            i.InstrumentStaticData.TradingScope));
                }
            }

            return true;
        }

        #endregion
    }
}